<?php
class testControl extends skymvc{
	
	public function __construct(){
		parent::__construct();
	}
	
	public function onDefault(){
		exit();
		$data=array(
			"key"=>"order",
			"para"=>array(1234)
		);
		$res=sendSms(15985840591,$data);
		print_r($res);
		echo "发送成功";
	}
	
	public function OnImSHop(){
		
		set_time_limit(1000);
		//店铺分类
		$cats=array(
			"美食",
			"娱乐",
			"保健",
			"酒店"
		);
		foreach($cats as $c){
			if(!$catid=M("category")->selectOne(array("fields"=>"catid","where"=>"cname='".$c."' AND model_id=".MODEL_SHOP_ID))){
				$catid=M("category")->insert(array(
					"cname"=>$c,
					"model_id"=>MODEL_SHOP_ID
				));
			}
			$catids[]=$catid;
		}
		
		//店铺
		$f=file_get_contents("test/data/shop.txt");
		$f=str_replace("　","",$f);
		$fs=explode("\r\n",$f);
		foreach($fs as $a){
			$arr[]=trim($a);
		}
		 
		foreach($arr as $key=>$v){
			if($key>10) break; 
			if(!$shopid=M("shop")->selectRow(array("fields"=>"shopid","where"=>"shopname='".$v."'"))){
				shuffle($catids);
				$shopid=M("shop")->insert(array(
					"shopname"=>$v,
					"dateline"=>time(),
					"catid"=>$catids[0],
					"status"=>2,
				));
				M("shop_data")->insert(array(
					"shopid"=>$shopid
				));
			}
			$shopids[]=$shopid;
		}
		 
		$shops=M("shop")->select(array("where"=>" status=2 "));
		$cats=array("吃的","喝的","炒的");
		$data=file_get_contents("test/data/shop_product.txt");
		$data=json_decode($data,true);
		$ct=count($data);
		foreach($data as $k=>$v){
			if($k<$ct/3){
				$sdata[0][]=$v;
			}elseif($k<$ct*2/3){
				$sdata[1][]=$v;
			}else{
				$sdata[2][]=$v;
			}
		}
		 
		foreach($shops as $shop){
			foreach($cats as $k=>$c){
				if(!$cid=M("shop_category")->selectOne(array(
					"where"=>"title='".$c."' AND shopid=".$shop['shopid'],
					"fields"=>"id"
				))){
					$cid=M("shop_category")->insert(array(
						"title"=>$c,
						"shopid"=>$shop['shopid']
					));
				}
				foreach($sdata[$k] as $in){
					$in['catid']=6;
					$in['shop_cid']=$cid;
					$in['status']=2;
					$in['shopid']=$shop['shopid'];
					M("shop_product")->insert($in);
				}
			}
		}
		echo "导入成功";
	}
	
	public function onEXPro(){
		
		$data=M("product")->select(array(
			"where"=>" status=2"
		));
		M("product")->close();
		set_time_limit(0);
		if($data){
			foreach($data as $v){
				
				$sdata[]=array(
					"title"=>$v['title'],
					"imgurl"=>$v['imgurl'],
					"price"=>$v['price']
				);
			}
			file_put_contents("temp/data.txt",json_encode($sdata));
		}
		
		echo "保存成功";
	}
	
	
	public function onYmCat(){
		$data=M("product")->select(array("where"=>"1=1"));
		foreach($data as $v){
			M("ymdian_product")->update(array("catid"=>$v['ymcatid']),"productid=".$v['id']);
		}
		echo "分类修正完毕";
	}
	
	public function onYmBuyNum(){
		exit();
		M("ymdian")->begin();
		$yms=M("ymdian")->select(array("where"=>" 1=1 "));
		foreach($yms as $ym){
			$oids[$ym['id']]=M("ymdian_order")->selectCols(array("where"=>" shopid=".$ym['id']." AND status=3"));
		}
		$ympros=M("ymdian_product")->select(array("where"=>"1=1"));
		foreach($ympros as $v){
		 
			$buy_num=0;
			if($oids[$v['shopid']]){
				$buy_num=M("ymdian_order_product")->selectOne(array("where"=>" productid=".$v['productid']." AND order_id in("._implode($oids[$v['shopid']]).")","fields"=>"sum(amount) as ct" ));
			}
			echo "\r\n---".$v['shopid']."--".$v['id']."----".$buy_num."---\r\n";
			M("ymdian_product")->update(array("buy_num"=>$buy_num),"id=".$v['id']);
		}
		M("ymdian")->commit();
		echo "更新成功";
	}
	
	/*
	*纠正 夜猫店统计
	*/
	public function onjzStat(){
		exit();
	 	M("ymdian")->begin();
		M("stat_shoporder_day")->delete("k='ymdian'");
		M("stat_shoporder_week")->delete("k='ymdian'");
		M("stat_shoporder_month")->delete("k='ymdian'");
	 	
		$yms=M("ymdian")->select(array("where"=>" 1=1 "));
		
		foreach($yms as $ym){
			$num=0;
 		 	/**统计
			* allmoney=已完成订单
			* allbalance= allstats-allmoney;
			**/
			$ods=M("ymdian_order")->select(array(
				"where"=>" status=3 AND shopid=".$ym['id'].""
			));
			if($ods){
				foreach($ods as $o){
					$money1=$o['money'];
					$money1_b=$money1*YM_GL_MONEY;
					$on_money1=$on_money2=$off_money1=$off_money2=0;
					if($o['ispay']==2){
						$on_money1=$money1;
						$on_money2=$money2;
					}else{
						$off_money1=$money1;
						$off_money2=$money2;
					}
					//添加今日
					$kday=date("Ymd",$o['dateline']);
					$rd=M("stat_shoporder_day")->selectRow(" shopid=".$o['shopid']." AND k='ymdian' AND kday='".$kday."' ");
					if($rd){
						M("stat_shoporder_day")->update(array(
							"money"=>$money1+$rd['money'],
							"ordernum"=>$rd['ordernum']+1,
							"balance"=>$money1_b+$rd['balance'],
							"on_money"=>$on_money1+$rd['on_money'],
							"off_money"=>$off_money1+$rd['off_money'],
						),"id=".$rd['id']);
					}else{
						M("stat_shoporder_day")->insert(array(
							"money"=>$money1,
							"balance"=>$money1_b+$rd['balance'],
							"ordernum"=>1,
							"kday"=>$kday,
							"k"=>"ymdian",
							"shopid"=>$o['shopid'],
							"on_money"=>$on_money1,
							"off_money"=>$off_money1,
						));
					}
					
					//添加本周收入
					$kweek=date("YW",$o['dateline']);
					$rd=M("stat_shoporder_week")->selectRow(" shopid=".$o['shopid']." AND k='ymdian' AND kweek='".$kweek."' ");
					if($rd){
						M("stat_shoporder_week")->update(array(
							"money"=>$money1+$rd['money'],
							"balance"=>$money1_b+$rd['balance'],
							"ordernum"=>$rd['ordernum']+1,
							"on_money"=>$on_money1+$rd['on_money'],
							"off_money"=>$off_money1+$rd['off_money'],
						),"id=".$rd['id']);
					}else{
						M("stat_shoporder_week")->insert(array(
							"money"=>$money1,
							"balance"=>$money1_b+$rd['balance'],
							"ordernum"=>1,
							"kweek"=>$kweek,
							"k"=>"ymdian",
							"shopid"=>$o['shopid'],
							"on_money"=>$on_money1,
							"off_money"=>$off_money1,
						));
					}
					
					//添加本月收入
					$kmonth=date("Ym",$o['dateline']);
					$rd=M("stat_shoporder_month")->selectRow(" shopid=".$o['shopid']." AND k='ymdian' AND kmonth='".$kmonth."' ");
					if($rd){
						M("stat_shoporder_month")->update(array(
							"money"=>$money1+$rd['money'],
							"balance"=>$money1_b+$rd['balance'],
							"ordernum"=>$rd['ordernum']+1,
							"on_money"=>$on_money1+$rd['on_money'],
							"off_money"=>$off_money1+$rd['off_money'],
						),"id=".$rd['id']);
					}else{
						M("stat_shoporder_month")->insert(array(
							"money"=>$money1,
							"balance"=>$money1_b+$rd['balance'],
							"ordernum"=>1,
							"kmonth"=>$kmonth,
							"k"=>"ymdian",
							"shopid"=>$o['shopid'],
							"on_money"=>$on_money1,
							"off_money"=>$off_money1,
						));
					}
					
					
				}
			}
			
			$allmoney=M("ymdian_order")->selectOne(array("where"=>" shopid=".$ym['id']." AND status=3 ","fields"=>"sum(money)"));
			$allstats= M("stat_shoporder_day")->selectOne(array("where"=>" shopid=".$ym['id']." AND k='ymdian' ","fields"=>"sum(money)"));
			$monthstats=M("stat_shoporder_day")->selectOne(array("where"=>" shopid=".$ym['id']." AND k='ymdian' ","fields"=>"sum(money)"));
			$balance=$allmoney*YM_GL_MONEY;
			M("ymdian")->update(array("balance"=>$balance,"income"=>$allmoney),"id=".$ym['id']);
			M("ymdian")->addMoney(array(
					"shopid"=>$ym['id'],
					"balance"=>0,
					"income"=>0,
					"content"=>"系统调整营业收入跟收益"
				));
			echo "\r\n--------".$ym['id']."--".$ym['title']."---all:".$allmoney."---stat:".$allstats."--month:".$monthstats."--balance:$balance--\r\n";
		}
		 M("ymdian_dakuan")->delete("1=1");
		 
		M("ymdian")->commit(); 
	}
	
	
	/*矫正夜猫店产品数量
	**产品数=进货成功-退货非取消-下单（非取消）
	*/
	public function onYmProduct(){
		exit();
		$yms=M("ymdian")->select(array("where"=>" 1=1 "));
		foreach($yms as $ym){
			/*kcorder*/
				$pros=M("ymdian_product")->getALl("select productid,total_num From ".table('ymdian_product')." WHERE shopid=".$ym['id']."  ");
				echo "--------夜猫店 ".$ym['title']."----------------------\r\n";
				$products=array();
				if($pros){
					foreach($pros as $p){
						$products[$p['productid']]=0;
					}
				}
				 
				//进货
				$oids_1=M("kcorder")->getCols("select order_id from ".table('kcorder')." WHERE typeid=1 AND shopid=".$ym['id']." AND isreceived=2 ");
				if($oids_1){
					$a1=M("kcorder_product")->getAll("select productid,sum(buy_num) as num from ".table('kcorder_product')." WHere order_id in("._implode($oids_1).") group by productid ");
					 
					 foreach($a1 as $a){
						$products[$a['productid']] +=$a['num']; 
					 }
					 echo "---------进货\r\n";
					print_r($products);
					 
				}
				
			/*kcback*/
				//下单
				$back_1=M("kcback")->getCols("select order_id from ".table('kcback')." WHERE typeid=1 AND shopid=".$ym['id']." AND status<4 ");
				if($back_1){
					$m2=M("kcback_product")->getAll("select productid,sum(buy_num) as num from ".table('kcback_product')." WHere order_id in("._implode($back_1).")  group by productid ");
					foreach($m2 as $a){
						$products[$a['productid']] -=$a['num']; 
					 }
					 echo "---------退货下单\r\n";
					print_r($products);
				}
				
				//取消
				$back_2=M("kcback")->getCols("select order_id from ".table('kcback')." WHERE typeid=1 AND shopid=".$ym['id']." AND status=10 ");
				if($back_2){
				 	$a2=M("kcback_product")->getAll("select productid,sum(buy_num) as num from ".table('kcback_product')." WHere order_id in("._implode($back_2).")  group by productid ");
					/**
					foreach($a2 as $a){
						$products[$a['productid']] +=$a['num']; 
					 }
					 echo "---------退货取消\r\n";
					print_r($products);
					**/
				}
				
			/*order*/
				//下单
				$order_1=M("ymdian_order")->getCols("select order_id from ".table('ymdian_order')." WHERE   shopid=".$ym['id']." AND status<4 ");
				if($order_1){
					$m3=M("ymdian_order_product")->getAll("select productid,sum(amount) as num from ".table('ymdian_order_product')." WHere order_id in("._implode($order_1).")  group by productid ");
					foreach($m3 as $a){
						$products[$a['productid']] -=$a['num']; 
					 }
					 echo "---------order下单\r\n";
					print_r($products);
				}
				
				//取消
				$order_2=M("ymdian_order")->getCols("select order_id from ".table('ymdian_order')." WHERE    shopid=".$ym['id']." AND status=10 ");
				if($order_2){
				 	$a2=M("ymdian_order_product")->getAll("select productid,sum(amount) as num from ".table('ymdian_order_product')." WHere order_id in("._implode($order_2).")  group by productid ");
					/**
					foreach($a2 as $a){
						$products[$a['productid']] +=$a['num']; 
					 }
					 echo "---------order取消\r\n";
					 print_r($products);
					 **/
				}
				
				if($products){
					foreach($products as $pid => $num){					  												
						M("ymdian_product")->update(array(
							"total_num"=>intval($num)
						),"shopid=".$ym['id']." AND productid=".$pid);
						
						M("ymdian_product")->total_num_add(array(
							"shopid"=>$ym['id'],
							"productid"=>$pid	,
							"total_num"=>0,
							"content"=>"[系统纠正]",
						));
					}
				}
				echo "\r\n---------------------------------------------------------------\r\n";
								
		}
	}
	
	/*阿里缓存测试*/
	public function onOCS(){
		
		$connect = new Memcached;  //声明一个新的memcached链接
		$connect->setOption(Memcached::OPT_COMPRESSION, false); //关闭压缩功能
		$connect->setOption(Memcached::OPT_BINARY_PROTOCOL, true); //使用binary二进制协议
		$connect->addServer('5aad8aec3a9d46fe.m.cnhzaliqshpub001.ocs.aliyuncs.com', 11211); //添加OCS实例地址及端口号
		$connect->setSaslAuthData('Lrj1234567', 'password'); //设置OCS帐号密码进行鉴权，如已开启免密码功能，则无需此步骤
		$connect->set("hello", "world");
		echo 'hello: ',$connect->get("hello");
		$connect->quit();
		
	}
	
	public function onKucun(){
		exit();
		$kclist=M("kucun")->select(array("where"=>"1=1"));
		foreach($kclist as $v){
			$jh=M("kcorder")->getOne(" select sum(gys_money) as money from ".table('kcorder')." where kcid=".$v['kcid']." AND status=3 ");
			$th=M("kcback")->getOne(" select sum(gys_money) as money from ".table('kcback')." where kcid=".$v['kcid']." AND status=3 ");
			echo $v['title']."账户余额".$v['balance'].": 进货: $jh,退货：-$th,总计：".($jh-$th)."<br>";
		}
	}
	
	public function onymBalance(){
		exit();
		$shops=M("ymdian")->select(array("where"=>"1=1"));
		if($shops){
			foreach($shops as $shop){
				$jh=M("kcorder")->getOne("select sum(money) from ".table('kcorder')." WHERE typeid=1 AND status<4 AND shopid=".$shop['id']." ");
				$th=M("kcback")->getOne("select sum(money) from ".table('kcback')." WHERE typeid=1 AND status=3 AND shopid=".$shop['id']." ");
				$od=M("ymdian_order")->getOne("select sum(money) from ".table('ymdian_order')." where status=3 ");
				$balance=intval($th)-intval($jh)+intval($od);
				M("ymdian")->update(array("balance"=>$balance),"id=".$shop['id']);
				echo "店铺".$shop['title']."余额：".$shop['balance']." 实际余额：".$balance."<br>";
				
			}
		}
	}
	
	
	public function onkdBalance(){
		exit();
		$shops=M("koudai")->select(array("where"=>"1=1"));
		if($shops){
			foreach($shops as $shop){
				$jh=M("kcorder")->getOne("select sum(money) from ".table('kcorder')." WHERE typeid=2 AND status<4 AND shopid=".$shop['id']." ");
				$th=M("kcback")->getOne("select sum(money) from ".table('kcback')." WHERE typeid=2 AND status=3 AND shopid=".$shop['id']." ");
				$od=M("koudai_order")->getOne("select sum(money) from ".table('koudai_order')." where status=3 ");
				$balance=intval($th)-intval($jh)+intval($od);
				M("koudai")->update(array("balance"=>$balance),"id=".$shop['id']);
				echo "店铺".$shop['title']."余额：".$shop['balance']." 实际余额：".$balance."<br>";
				
			}
		}
	}
	
	
	public function onClearAll(){
		exit();
		$query=M("article")->query("show tables ");
		$da=M("article")->fetch_array($query,PDO::FETCH_NUM);
		if($da){
			foreach($da as $v){
				$data[]=str_replace(TABLE_PRE,"",$v[0]);
			}
		}
		$un=array(
			'ad','ad_tags','admin','admin_group','attribute','attribute_cat','category','config','dataapi','model','navbar','options','permission','seo','sites','district'
		); 
		if($data){
			foreach($data as $k=>$v){
				if(in_array($v,$un)){
					unset($data[$k]);
				}
			}
		}
		 
		foreach($data as $v){
			M($v)->delete("1=1");
		}
		echo "清空完毕";
	}
	
	public function onDelKucun(){
		
		exit();
	 
	}
	
}
?>